// pages/index/qingjia/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode: '',
    picture:'',
    datetimeVisible: false,
    datetime: new Date('2023-05-23').getTime(),
    datetimeText: '',
    enddatetimeVisible: false,
    enddatetime: new Date('2023-05-23').getTime(),
    enddatetimeText: '',
    fileList: [],
    cityVisible:false,
    cityText: '',
    cityValue: [],
    citys: [],
    reason:'',
    teacherId:'',
    teacherName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'http://localhost:9090/api/user/getTeacherList',
      method:'get',
      success:res=>{
        if(res.data.code == 200){
          res.data.data.forEach(element => {
            element.label=element.name
            element.value=element.name
          });
          this.setData({
            citys:res.data.data
          })
        }
      }

    })
  },
  // 选择老师
  onCityPicker() {
    this.setData({ cityVisible: true });
  },

  onColumnChange(e) {
    console.log('picker pick:', e);
    
    console.log(this.data.teacherId)
  },

  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    console.log('picker change:',e);
    this.setData({
      teacherId:this.data.citys[e.detail.columns[0].index].id,
      teacherName:this.data.citys[e.detail.columns[0].index].name
    })
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
     
    });
  },

  onPickerCancel(e) {
    const { key } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  // 原因
  inputRegiosn(e){
    this.setData({
      reason:e.detail.value
    })
  },
  // 选择开始时间
  showPicker(e) {
    const { mode } = e?.currentTarget?.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e?.detail;
    const { mode } = this.data;
    console.log('confim', value);
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });
    this.hidePicker();
  },
  onColumnChange(e) {
    console.log('pick', e?.detail?.value);
  },

  //结束时间
  showEndPicker(e) {
    if(this.data.datetimeText ==''){
      wx.showToast({
        title: '请先选择开始时间！',
        icon:'none'
      })
      return
    }
    console.log(e)
    const { mode } = e?.currentTarget?.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hideEndPicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirmEnd(e) {
    
    const { value } = e?.detail;
    const { mode } = this.data;
    console.log('confim', value);
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });
    this.hidePicker();
  },
  onColumnEndChange(e) {
    console.log('pick', e?.detail?.value);
  },
  // 上传图片
  handleAdd(e) {
    const { fileList } = this.data;
    const { files } = e.detail;
    console.log(e)
    // 方法1：选择完所有图片之后，统一上传，因此选择完就直接展示
    this.setData({
      fileList: [...fileList, ...files], // 此时设置了 fileList 之后才会展示选择的图片
    });
    wx.uploadFile({
      url: 'http://localhost:9090/api/file/upload', // 仅为示例，非真实的接口地址
      filePath: files[0].url,
      name: 'file',
      formData: { user: 'test' },
      success: res => {
        this.setData({
          picture: res.data,
        });
      },
    });
    // 方法2：每次选择图片都上传，展示每次上传图片的进度
    // files.forEach(file => this.uploadFile(file))
  },
  handleRemove(e){
    const { index } = e.detail;
    const { fileList } = this.data;

    fileList.splice(index, 1);
    this.setData({
      fileList,
      picture:''
    });
  },
  // 提交
  submitClick(){
    if(this.data.datetimeText == ''){
      wx.showToast({
        title: '请选择开始时间！',
        icon:'none'
      })
      return
    }else if(this.data.enddatetimeText == ''){
      wx.showToast({
        title: '请选择结束时间！',
        icon:'none'
      })
      return
    }else if(this.data.picture == ''){
      wx.showToast({
        title: '请上传图片',
        icon:'none'
      })
      return
    }else if(this.data.reason == ''){

     
      wx.showToast({
        title: '请输入请假理由！',
        icon:'none'
      })
      return
    }
    if ( this.data.state ) {
      wx.showToast({
        title: '正在提交',
        icon:'none'
      })
      return ;
    }
    this.setData({
      state:true
    })
    wx.request({
      url: 'http://localhost:9090/api/pleaseLeave/pleaseLeaveInsert',
      data:{
        userId:wx.getStorageSync('userInfo').user.id,
        userName:wx.getStorageSync('userInfo').user.name,
        teacherId: this.data.teacherId,
        teacherName: this.data.teacherName,
        reason:this.data.reason,
        picture: this.data.picture,
        startTime:this.data.datetimeText,
        endTime: this.data.enddatetimeText,
        state:false,
      },
      method:'POST',
      success:res=>{
        if(res.data.code == 200){
          wx.showToast({
            title: '提交成功！',
            icon:'none'
          })
          setTimeout(() => {
              wx.navigateBack({delta:1})
          }, 1000);
        }
        this.setData({
          state:false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})